package com.cjjd.c.feature.mine.membercard;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.BaseSwipeRefreshFragment;
import com.cjjd.c.base.MyBaseAdapter;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.ListResult;
import com.cjjd.c.bean.MemberCardDetailItemBean;
import com.cjjd.c.bean.MemberCardItemBean;
import com.cjjd.c.feature.hkb.ExchangeHkGoldActivity;
import com.cjjd.c.feature.mine.LevelFragment;
import com.cjjd.c.feature.mine.benefits.BenefitsActivity;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.FragmentUtil;
import com.cjjd.c.utils.ImageUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/15.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class MemberCardDetailFragment extends BaseSwipeRefreshFragment {

    private TextView textView;
    private MemberCardItemBean memberCardItemBean;
    private List<MemberCardDetailItemBean> dataList = new ArrayList<>();

    @Override
    public Object getTitleString() {
        return "会员卡详情";
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }

    @Override
    public void responseSuccess(Call<HttpResult> call, Response<HttpResult> response) {

        if (textView != null && listBase.getFooterViewsCount() == 0) {
            listBase.addFooterView(textView);
        }

        ListResult<MemberCardDetailItemBean> listResult = (ListResult<MemberCardDetailItemBean>) response.body().getData();
        if (dataList.size() > 0) {
            dataList.clear();
        }
        dataList.addAll(listResult.dataList);
        setDataList(dataList);
    }

    @Override
    protected void initView() {
        super.initView();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_header_membercard_detail, null);
        ImageView ivCard = (ImageView) view.findViewById(R.id.iv_card);
        TextView tvCurrentPoint = (TextView) view.findViewById(R.id.tv_current_point);

        // 兑换好客币
        TextView tvHkb = (TextView) view.findViewById(R.id.tv_hkb);
        tvHkb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                FragmentUtil.replaceWithAddToBack(mContext, new LevelFragment(), R.id.activity_body);
                if(memberCardItemBean!=null) {
                    ExchangeHkGoldActivity.lauch(mContext,memberCardItemBean.uuid);
                }
            }
        });
        // 会员福利
        TextView tvHyfl = (TextView) view.findViewById(R.id.tv_hyfl);
        tvHyfl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                FragmentUtil.replaceWithAddToBack(mContext, new LevelFragment(), R.id.activity_body);
                BenefitsActivity.lauch(mContext,memberCardItemBean.uuid,memberCardItemBean.imageUrl,memberCardItemBean.cardName);
            }
        });

        TextView tv_jfsc=(TextView)view.findViewById(R.id.tv_jfsc);
        tv_jfsc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast("积分商城暂未开通哦");
            }
        });



        ImageUtil.displayImage(memberCardItemBean.imageUrl, ivCard);
        tvCurrentPoint.setText("当前积分： " + memberCardItemBean.cardScore);


//        textView = new TextView(mContext);
//        textView.setText("没有更多记录啦~");
//        textView.setGravity(Gravity.CENTER);
//        textView.setTextColor(getResources().getColor(R.color.textColorNomal));
//        textView.setPadding(30, 30, 30, 30);
        listBase.addHeaderView(view);
//        listBase.addFooterView(textView);

        listBase.setDivider(null);

    }

    @Override
    public MyBaseAdapter getAdapter() {
        return new MemberCardDetailAdapter(mContext);
    }

    @Override
    public Call<HttpResult> getCall() {

        getHeadMessage();

        if (textView != null && listBase.getFooterViewsCount() > 0) {
            listBase.removeFooterView(textView);
        }
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("nowPage", PAGE + "");
        h1.put("pageShow", PAGE_SIZE + "");
        h1.put("uuid", memberCardItemBean.uuid);

        return service.getCustomerCardIntegralLog(JsonUtils.convertParam("getCustomerCardIntegralLog", h1));

    }

    private void getHeadMessage() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("uuid", memberCardItemBean.uuid);

        Call call = service.getCustomerCardDetail(JsonUtils.convertParam("getCustomerCardDetail", h1));
        call.enqueue(new CallbackImpl() {
            @Override
            public void responseSuccess(Call call, Response response) {

            }
        });
    }

    @Override
    public void initRightButton(Button rightButton) {
        super.initRightButton(rightButton);
        /*rightButton.setVisibility(View.GONE);
        rightButton.setText("会员卡说明");
        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });*/
    }

    public void setDetail(MemberCardItemBean memberCardItemBean) {
        this.memberCardItemBean = memberCardItemBean;
    }
}
